<!-- 生成订单--收货地址 -->
<template>
  <div>
    <van-field
      readonly
      clickable
      label="收货地址"
      :value="defaultAddress"
      placeholder="选择收货地址"
      @click="showPicker = true"
    />
    <van-popup v-model="showPicker" position="bottom">
      <van-picker
        show-toolbar
        :columns="columns"
        @confirm="onConfirm "
        @cancel="showPicker = false"
      />
    </van-popup>
  </div>
</template>

<script>
export default {
  data() {
    return {
      showPicker: false, //弹窗

      defaultAddress: "", // 默认地址 展示
      columns: [], //我的地址
      changeUser: [],
      cityId: [],
      ReceivingAddressID: [],
      provinceId: [],
      selectprvinceid: "",
      getAddressData: ""
    };
  },
  methods: {
    onConfirm(value, index) {
      console.log("确认选择该地址", value, index);
      this.defaultAddress = value;
      this.showPicker = false;
      this.selectprvinceid = this.provinceId[index];
      let myuser = this.changeUser[index];
      let mycityid = this.cityId[index];
      this.$emit("addressChanges", value, myuser, mycityid);
      console.log(value,"|", myuser,"|", mycityid,"|", this.provinceId[index]);
    },
    // onCancel() {
    //   this.showPicker = false;
    //   this.$router.push({
    //     path: "/myaddress"
    //   });
    //   console.log("新增收货地址");
    // },

    //获取收货地址
    async getAddress() {
      let user_id = window.localStorage.getItem("user_id");
      let address = await this.$http({
        method: "post",
        url: "home/index/getaddresslist",
        data: {
          user_id,
          type: "1"
        }
      });

      if (address.data.State.Code == 200) {
        this.getAddressData = address.data.data;
        // console.log("获取收货地址", this.getAddressData);

        this.getAddressData.forEach((item, index) => {
          if (item.is_default == 1) {
            // console.log("默认选择");

            // this.defaultAddress = `${item.province_name}${item.city_name} ${item.district_name} ${item.address} ${item.consignee} `;
            // console.log(this.defaultAddress);
            // this.selectprvinceid = item.province_id;
            this.$emit("addressChanges", this.defaultAddress, item.consignee, item.city_id);
          }
          let getAddress = `${item.province_name}${item.city_name} ${item.district_name} ${item.address} ${item.consignee} `;
          let consignees = `${item.consignee}`;
          // console.log(this.getAddressData);
          this.columns.push(getAddress);
          this.changeUser.push(consignees);
          // console.log(this.columns, "全部收货地址");
          // console.log(this.changeUser, "全部用户名");

          let city_id = item.city_id;
          this.cityId.push(city_id);
          // console.log(this.cityId, "全部收货城市id 提交订单");

          let province_id = item.province_id; //收货省份id 判断运费
          this.provinceId.push(province_id);
          // console.log(this.provinceId, "全部收货省份id 判断运费");
        });
      }
    },
    //获取运费
    getFreight() {
      let goods_info = [];
      let province_id = this.selectprvinceid; // 省份id
      let goods = this.$store.state.SettlementData; //購物車數量
      goods.forEach((item, index) => {
        goods_info.push({ id: item.id, province_id, num: item.num });
      });
      console.log(goods_info);
      let res = this.$http({
        method: "post",
        url: "home/settlement/getexpress",
        data: {
          goods_info
        }
      })
        .then(res => {
          // console.log(res.data.price,'dsada');
          let freight = res.data.price;
          this.$emit("adFreight", freight);
          console.log(freight, "dasdadadada");
          // this.$store.commit("getFreight", res.data.price);
          // console.log(this.$store.state.getFreight)
        })
        .catch(err => {
          console.log(err);
        });
    }
  },
  created() {
    this.getAddress();
  },
  watch: {
    selectprvinceid(oldvalue, newvalue) {
      this.getFreight();
    }
  }
};
</script>
<style lang='scss' scoped>
</style>